package com.zx.cloud.order.controller;


import com.zx.cloud.order.service.OrderTblService;

import org.bouncycastle.math.Primes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

import javax.servlet.http.HttpServletRequest;

import io.netty.util.concurrent.FastThreadLocal;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhaoxuan
 * @since 2021-12-22
 */
@RestController
@RequestMapping("/orderTbl")
public class OrderTblController {

    @Autowired
    private OrderTblService orderTblService;


    /**
     * 下单：插入订单表、扣减库存，模拟回滚
     *
     * @return
     */
    @GetMapping("/placeOrder/commit")
    public Boolean placeOrderCommit(String userId,String code,Integer count) {

        orderTblService.placeOrder(userId, code, count);
        return true;
    }

    /**
     * 下单：插入订单表、扣减库存，模拟回滚
     *
     * @return
     */
    @GetMapping("/test")
    public Boolean test() throws ExecutionException, InterruptedException {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        RequestContextHolder.setRequestAttributes(requestAttributes,true);
        FutureTask<Object> task = new FutureTask<>(() -> {
            ServletRequestAttributes requestAttributes1 = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = requestAttributes1.getRequest();
            return request;
        });
        new Thread(task).start();
        //HttpServletRequest request = (HttpServletRequest)task.get();
        return true;
    }

    public static final FastThreadLocal<String> stringFastThreadLocal = new FastThreadLocal<>();

    public static class TestCaallble<String> implements Callable<String>{
        public String xid;

        /**
         * Computes a result, or throws an exception if unable to do so.
         *
         * @return computed result
         * @throws Exception if unable to compute a result
         */

        public TestCaallble(){
            this.xid=(String) stringFastThreadLocal.get();
        }

        @Override
        public String call() throws Exception {
            return xid;
        }
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        stringFastThreadLocal.set("123456");
        FutureTask futureTask = new FutureTask<>(new TestCaallble<>());
        new Thread(futureTask).start();
        System.out.println(futureTask.get());
    }
}

